<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );
require_once( '../../libs/page.php' );

if($_SERVER['REQUEST_METHOD'] == 'GET'){
	
	$order_id = $_GET['order_id'];
	$db = new Class_DB;
	$sql = "SELECT * FROM k_order_head WHERE order_id = '$order_id'";
	$result = $db->query($sql);
	$order = mysql_fetch_array($result);
	
	$sql = "SELECT * FROM k_order_detail as a WHERE a.order_id='$order_id'";
	$result = $db->query($sql);
	$data = array();
	$m1 = 0;
	while( $row = mysql_fetch_array($result,MYSQL_ASSOC) ){
		$row['money'] = $row['refer_price']*$row['quantity'];
		$m1 = $m1+$row['money'];
		$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
			$row['brand'],
			$row['shop_name'],
			$row['style'],
			$row['refer_price'],
			$row['quantity'],
			$row['money']
		),
		"sjis",
		"utf8"
	);
	}
	
	$m2 = $m1 - $m1*$order['discount'];
	$m3 = $order['shipping'];
	$m4 = round(($m1*$order['discount']+$m3)*$order['tax'],0);
	$m5 = $m1-$m2+$m3+$m4;
	$smarty->assign('m1', $m1);
	$smarty->assign('m2', $m2);
	$smarty->assign('m3', $m3);
	$smarty->assign('m4', $m4);
	$smarty->assign('m5', $m5);
	
	
	$category="";
	if ($order['category'] == 1 )  $category = "プリント工房PRO";
	if ($order['category'] == 2 )  $category = "プリント工房LITE";
	if ($order['category'] == 3 )  $category = "名刺工房";
	if ($order['category'] == 4 )  $category = "アルバム工房";
	
	$filename = date("YmdHis") . '.csv';
	header("Content-Type: application/octet-stream");
	header('Content-Disposition: attachment; filename=' . $filename );

	print mb_convert_encoding( "受注日,".$order['o_date'], "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "サービス名,".$category, "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "氏名,".$order['name'], "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "住所,".$order['address'], "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "メールアドレス,".$order['email'], "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "納品日,".$order['delivery_date'], "sjis", "utf8");
	print "\n";
	print "\n";
	print "\n";
	print mb_convert_encoding("ブランド名,商品名,サイズ,単価,数量,金額", "sjis", "utf8");
	print "\n";
	print implode( $data, "\n" );
	print "\n";
	print "\n";
	print "\n";
	print mb_convert_encoding( "小計,"."￥$m1", "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "お値引き,"."￥$m2", "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "送料,"."￥$m3", "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "消費税,"."￥$m4", "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "合計,"."￥$m5", "sjis", "utf8");
	print "\n";
	print "\n";
	print "\n";
	print mb_convert_encoding( "備考,".$order['quote_remark'], "sjis", "utf8");
	print "\n";
	print mb_convert_encoding( "特価理由,".$order['quote_special'], "sjis", "utf8");
	
}


?>